<?php


namespace App\Http\Controllers\user;

use App\Engine\TokenService;
use App\Exceptions\ApiException;
use App\Http\service\XcxService;
use App\Jobs\DelChartLogJob;
use App\Models\ChartLog;
use App\Models\TxLog;
use App\Models\User as UserModel;
use App\Engine\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use function app\Utils\go;

class User extends BaseController
{
    private $model;


    public function __construct(Request $request)
    {
        $this->model=new UserModel();
        parent::__construct($request);
    }

    /**获取个人信息
     * @return array
     */
    public function get_infor()
    {
        $res=$this->model->with(['custom'])->where("id",TokenService::getCurrentUid())->first();
        return go($res);
    }

    /**更新用户个人信息
     * @return array
     */
    public function update_infor()
    {
        $res=$this->model->find(TokenService::getCurrentUid())->update(["nickname"=>$this->post["nickname"],"avatar"=>$this->post["avatar"]]);
        return go($res);
    }

    /**
     * 互获取我的邀请
     */
    public function get_my_invite()
    {
        $res=$this->model->where("invite_uid",TokenService::getCurrentUid())->select(["id","nickname","avatar","created_at"])->paginate($this->get["num"]);
        return go($res);
    }

    public function send_msg()
    {
        $res=(new XcxService())->send_temp();
        return go($res);
    }

    public function get_message()
    {
        $res=ChartLog::where("message_id",$this->get["message_id"])->where("is_delete",null)->orderBy("id","desc")->paginate($this->get["num"]);
        return go($res);
    }

    /**删除聊天记录
     * @return array
     */
    public function del_talking()
    {
        DelChartLogJob::dispatch($this->get["message_id"]);
        $res=ChartLog::where("message_id",$this->get["message_id"])->update(["is_delete"=>1]);
        DelChartLogJob::dispatch($this->get["message_id"]);
        return go($res);
    }

    /**创建提现申请二维码
     * @return array
     * @throws \App\Exceptions\ApiException
     */
    public function create_tx_qrcode()
    {
        $txModel=new TxLog();
        $uid=TokenService::getCurrentUid();
        $user=UserModel::where("id",$uid)->first();
        if ($user["balance"]<$this->get["price"])
            throw new ApiException("余额不足");
        DB::beginTransaction();
        try {
            $create=[
                "uid"=>TokenService::getCurrentUid(),
                "price"=>$this->get["price"],
                "state"=>0,
            ];
            $res=$txModel->create($create);
            $scene="type=tx&id=".$res["id"];
            $qrcode=(new XcxService())->create_xcx_code($scene);
           $txModel->where("id",$res["id"])->update(["qrcode"=>$qrcode]);
           $res=$txModel->where("id",$res["id"])->first();
            DB::commit();
            return go($res);
        }catch (\Exception $exception){
            DB::rollBack();
            throw new ApiException("申请失败");
        }


    }
}
